<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/*
build_component_xml.php
This is called only at creating a new component and not any where else..
*/


if(!isset($semf_root) )
{
	// Loading all the components
	$component_type=$_GET['component'];
	$component_id=$_GET['id'];
	$component_container=$_GET['container'];
	$page_xml = $_GET['page_xml'];
	$current_property_view = $_GET['$current_property_view'];

	$semf_root = '../';
	// Define that we are returning XML content & not to cache:
	header('Content-Type: text/xml');
	header('Cache-control: no-cache');
}

include_once($semf_root."page_designer/util/dirutils.php");
include_once($semf_root."page_designer/util/backup.php");
include_once($semf_root."page_designer/util/build_component_xml_util.php");
include_once($semf_root."page_designer/components/${component_type}/component.php");

$class_name = ${component_type}."Component";

$obj = new ${class_name}();


// build the xml
$doc = NULL;
$parent = NULL;

//create a new document

if(isset($from_plugin) && $from_plugin == 1)
{
	$doc = $page_doc;
	$parent = $doc->getElementById($component_container);
}
elseif( !isset($component_container) || $component_container == '' )
{
	// Create an instance of the DomImplementation class
	$impl = new DomImplementation;
	// Create an instance of the DomDocumentType class by
	// calling the DomImplementation::createDocumentType() method
	// (arguments: doctype_name, doctype_public, doctype_system)
	$relative_dir = find_relative_dir($page_xml, $semf_root."page_designer/"."page.dtd");
	$dtd = $impl->createDocumentType("page", "", $relative_dir);
	// Create an instance of the DomDocument class by
	// calling the DomImplementation::createDocument() method
	// (arguments: NS_URL, NS_PREFIX, DomDocumentType_instance)
	$doc = $impl->createDocument("", "", $dtd);
	// Set other properties
	$doc->encoding = "UTF-8";
	$doc->standalone = "no";
	
	$root = $doc->createElement('page');
	$doc-> appendChild($root);
	
	$parent = $root;

}
else
{
	$doc = new DOMDocument();
	if (file_exists($page_xml)) {
		$doc->load($page_xml);
	}
	
	$doc->validate();
	
	$parent = $doc->getElementById($component_container);

}

$our_component = $doc->createElement('component');
$our_component-> setAttribute('type', $component_type);
$our_component-> setAttribute('id', $component_id);

//set properties under the node..

$properties = $obj-> get_properties();

$html_properties = array("page_xml"=> $page_xml);

$properties = $obj-> save_html_properties($properties, $html_properties);

// build the dynamic properties..
$properties = $obj-> build_properties($parent, $properties, NULL, NULL, NULL);

//adjust the properties with the container..
/*echo "<before>\n";
print_r($properties);
echo "</before>\n";*/
$properties = $obj-> adjust_properties_with_container($parent, $properties);
/*echo "<after>\n";
print_r($properties);
echo "</after>\n";*/

// create the xml tags for the properies..
render_properties($our_component, $properties, $doc);

$parent-> appendChild($our_component);

//add containers
$containers = $obj->build_containers($parent, $properties);

render_containers($our_component, $containers, $doc);

//$doc->save($page_xml);
if(!isset($from_plugin))
{
	savePageXML($doc, $page_xml);
}

$result_dir = $semf_root."store/other/";
if(!is_dir($result_dir))
{
	mkdir($result_dir);
}
$component_dir = $semf_root."page_designer/components/${component_type}";
$copy_text = copy_include_files($result_dir, $component_dir);

if($copy_text == -1)
{
	$copy_text = "";
}
if(!isset($from_plugin))
{
	echo "<message>";
	
	echo $copy_text; //hope no garbages will return.
	echo "<option id=\"propertiesView\">";
	include("render_properties.php");
	echo "</option>";
	
	echo "<set id=\"designerView\">";
	include("render_editor.php");
	echo "</set>";
	
	echo "<exec>";
	include("build_component_containers.php");
	echo "</exec>";
	
	echo "</message>";
}

if(isset($from_plugin))
{
	$page_doc = $doc;
}

?>